package pers.jsan.litheopen.security.filter.login;

import pers.jsan.litheopen.security.exception.LitheAuthenticationException;
import pers.jsan.litheopen.security.filter.AuthenticationFilterChain;
import pers.jsan.litheopen.security.filter.LitheAuthenticationFilter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetSessionFilter implements LitheAuthenticationFilter {

    private final static String SESSION = "SESSION";

    @Override
    public void doFilter(HttpServletRequest servletRequest, HttpServletResponse servletResponse, AuthenticationFilterChain filterChain) throws IOException, ServletException, LitheAuthenticationException {
        Cookie[] cookies = servletRequest.getCookies();
        if(null != cookies) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+": "+cookie.getValue()+": "+cookie.getDomain());
                if(SESSION.equals(cookie.getName())) {
                    servletRequest.setAttribute(SESSION, cookie.getValue());
                    break;
                }
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
